package com.ydgk.servlet;

import com.ydgk.bean.Pclass;
import com.ydgk.bean.User;
import com.ydgk.dao.BaseDao;
import com.ydgk.service.PclassService;
import com.ydgk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PclassServlet", urlPatterns = "/PclassServlet.action")
public class PclassServlet extends BaseServlet{
    protected void findPclass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pn=request.getParameter("pageNumber");
        PclassService pclassService=new PclassService();
        Integer pageNumber=null;
        try{
            pageNumber = Integer.parseInt(pn);
        }catch (NumberFormatException e){
            pageNumber = 1 ;
        }
        Map map=pclassService.AdminSearch(pageNumber);
        List<Pclass> pclassList=(List<Pclass>)map.get("pclassList");
        String pageCode=(String)map.get("pageCode");
        for (Pclass pclass : pclassList) {
            System.out.println("pclass="+pclass);
        }
        System.out.println("pageCode="+pageCode);
        request.setAttribute("pageCode",pageCode);
        request.setAttribute("pclassList",pclassList);
        request.setAttribute("s","3");
        request.getRequestDispatcher("classificationManagement.jsp").forward(request,response);
    }
    protected void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = null ;
        try{
            id=Integer.parseInt(request.getParameter("id"));
        }catch (NumberFormatException e){
        }
        String className=request.getParameter("className");
        Pclass pclass=new Pclass();
        pclass.setId(id);
        pclass.setClassName(className);
        PclassService pclassService=new PclassService();
        int i = pclassService.saveOrUpdate(pclass);
        if(i>0){
            response.sendRedirect("PclassServlet.action?flag=findPclass");
        }else{
            response.getWriter().print("操作失败");
        }
    }
    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Pclass pclass=new Pclass();
        PclassService pclassService=new PclassService();
        String id=request.getParameter("id");
        Integer i=null;
        try {
            i= pclassService.deleteById(Integer.parseInt(id));
        }catch (NumberFormatException e){
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
    protected void deletes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Pclass pclass=new Pclass();
        PclassService pclassService=new PclassService();
        String ids=request.getParameter("ids");
        String[] s=ids.split(",");
        Integer i=null;
        for (String s1 : s) {
            try{
                i=pclassService.deleteById(Integer.parseInt(s1));
            }catch (NumberFormatException e) {
            }
            System.out.println(i);
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
}
